﻿using System.Runtime.Serialization;

namespace Eflong.MonitoringLib.MonitoringUnit
{
    [DataContract]
    public class MonitoringUnitType
    {
        public enum Number { NumOfTypes = 44 };// 22};

        /*    3、传感器值类型编码：
    表3
       类型	编码	类型	编码	类型	编码
       模拟量	1   	开关量	2   	控制量	3
       计数量	4    	累计量	5
   */

        public enum SensorTypes
        {
            模拟量 = 1 , 开关量 = 2 , 控制量 = 3 ,
            计数量 = 4 , 累计量 = 5 , 调节量 = 6
        }

        //public enum Types {DontCare = 0, CH4, S, T, CO, FDS, PowerBreaker, RunStop, OnOff, WindFlow,
        //     NP, P, SPower, Blower, Smog, P6000, InteMining, EleMachine, Strap, WindDoor,
        //     ElePower, WaterPump,MainBlower, LocalBlower, WindConduit ,CO2, HCH4, Voltage, Electricity,  Power,
        //     NoPower, PowerEnergy , O2 , WaterLevel, MineLevel, Highe, Displacement, Weight, Times, Counter,
        //     Simu, Addup, Contral, Borderline }; // Convert name to type;

        /// <summary>
        /// 传感器类型的枚举集合
        /// </summary>
        public enum Types
        {
            无 = 0 , 甲烷 = 1 , 瓦斯 = 1 , 风速 = 2 , 温度 = 3 , 一氧化碳 = 4 , CO = 4 ,
            分站 = 5 , 断电 = 6 , 开停 = 7 , 开关 = 8 , 风量 = 9 ,
            负压 = 10 , 压力 = 11 , 流量 = 12 , 风机 = 13 , 烟雾 = 14 , 控制 = 15 , 控制器 = 15 ,
            继电器 = 16 , 闭锁 = 16 , 供电 = 17 , 皮带 = 18 , 风门 = 19 ,
            馈电 = 20 , 馈电器 = 20 , 水泵 = 21 , 主扇 = 22 , 局扇 = 23 , 风筒 = 24 ,
            CO2 = 25 , 二氧化碳 = 25 , 高浓甲烷 = 26 , 高浓瓦斯 = 26 , 高浓度瓦斯 = 26 , 电压 = 27 , 电流 = 28 , 功率 = 29 ,
            无功功率 = 30 , 功率因数 = 31 , 氧气 = 32 , O2 = 32 , 水位 = 33 , 煤位 = 34 , 高度 = 35 , 位移 = 36 ,
            重量 = 37 , 计时 = 38 , 计数 = 39 ,
            模拟量 = 40 , 开关量 = 41 , 控制量 = 42 , 累计量 = 43 , 采煤机 = 44 ,
            抽放泵 = 45 , 粉尘 = 46 , 顶板压力 = 47 , 速度 = 48 , 硫化氢 = 49 , H2S = 49 ,
            湿度 = 50 , 勾数 = 51 , 混量 = 52 , 纯量 = 53 , 产量 = 54 , 人数 = 55 , 压风机 = 56 , 运输机 = 57 , 给煤机 = 58 , 提升机 = 59 , 刀闸 = 60 ,
            状态 = 61 , 报警 = 62 , 故障 = 63 , 堵煤 = 64 , 纵撕 = 65 , 跑偏 = 66 , 超速 = 67 , 超时 = 68 , 超员 = 69 , 越界 = 70 , 浓度 = 71 , 频率 = 72 ,
            全部模 = 99 , 全部开 = 100 , 电量 = 74
        };

        /*
         *  "采煤机","抽放泵", "粉尘", "顶板压力", "速度", "硫化氢",
            "湿度", "勾数", "混量", "纯量", "产量", "人数", "压风机","运输机", "给煤机", "提升机",
         public enum DigitalTypes
         {
             开停 = 40, 分站 = 41, 断电 = 42, 断电器 = 42,
             控制器 = 43, 控制 = 43, 闭锁 = 44, 继电器 = 44,
             风机 = 45, 风门 = 46, 馈电 = 47, 馈电器 = 47, 烟雾 = 48, 风筒 = 49,
             抽放泵 = 50, 水泵 = 51, 供电 = 52, 皮带 = 53, 采煤机 = 54,
             开关 = 55, 主扇 = 56, 局扇 = 57, 压风机 = 58,
             机组 = 59, 界闭 = 60
         };

         public enum AnalogTypes
         {
             甲烷 = 1, 瓦斯 = 1, 风速 = 2, 温度 = 3, CO = 4, 一氧化碳 = 4,
             风量 = 5, 负压 = 6, 管道压力 = 7, 差压 = 7, 顶板压力 = 8, 流量 = 9,
             高浓甲烷 = 10,高浓瓦斯 = 10,  氧气 = 11, CO2 = 12,
             粉尘 = 13, 粉尘 = 13, H2S = 14, 硫化氢 = 14,
             湿度 = 15, 速度 = 16, 水位 = 17, 煤位 = 18, 高度 = 19, 顶板位移 = 20,
             重量 = 22, 计时 = 23, 计数 = 24, 人数 = 25, 勾数 = 26,
             电压 = 27, 电流 = 28, 功率 = 29, 电量 = 30, 有功 = 31,无功 = 32,
             混量 = 33, 纯量 = 34, 产量 = 35
         };*/

        public static Types GetTypesByTypeName( string sTypeName )
        {
            int count = ( int )Types.提升机 + 1;
            Types type = Types.无;
            int n;
            for ( n = 1; n < count; n++ )
            {
                type = ( Types )n;
                if ( sTypeName == type.ToString( ) )
                {
                    break;
                }
            }
            if ( n == count ) type = Types.无;
            return type;
        }

        public static Types GetTypesByName( string sTypeName )
        {
            Types type = GetTypesByTypeName( sTypeName );
            if ( type != Types.无 ) return type;

            type = Types.模拟量;

            switch ( sTypeName.ToUpper( ) )
            {
                case "甲烷":
                    type = Types.甲烷;
                    break;

                case "一氧化碳":
                    type = Types.一氧化碳;
                    break;

                case "风速":
                    type = Types.风速;
                    break;

                case "温度":
                    type = Types.温度;
                    break;

                case "压力":
                    type = Types.压力;
                    break;

                case "负压":
                    type = Types.负压;
                    break;

                case "风量":
                    type = Types.风量;
                    break;

                case "全部模":
                    type = Types.全部模;
                    break;

                case "主扇":
                    type = Types.主扇;
                    break;

                case "局扇":
                    type = Types.局扇;
                    break;

                case "断电":
                case "断电器":
                    type = Types.断电;
                    break;

                case "烟雾":
                    type = Types.烟雾;
                    break;

                case "风门":
                    type = Types.风门;
                    break;

                case "风筒":
                    type = Types.风筒;
                    break;

                case "分站":
                    type = Types.分站;
                    break;

                case "控制":
                case "控制器":
                    type = Types.控制;
                    break;

                case "馈电":
                case "馈电器":
                    type = Types.馈电;
                    break;

                case "闭锁":
                case "继电器":
                    type = Types.闭锁;
                    break;

                case "全部开":
                    type = Types.全部开;
                    break;

                default:
                    type = Types.开停;
                    break;
            }

            return type;
        }

        public static Types GetMonitoringUnitTypeByName( string name , ref SensorTypes type , ref string unit , ref string[ ] strStates , ref bool m_bTriState )
        {
            Types typeName;
            switch ( type )
            {
                case SensorTypes.开关量:
                    typeName = Types.开停;
                    break;

                case SensorTypes.控制量:
                    typeName = Types.控制;
                    break;

                default:
                    typeName = Types.模拟量;
                    break;
            }

            if ( type == SensorTypes.控制量 )
            {
                typeName = Types.控制;

                //type = SensorTypes.开关量;
                strStates[ 0 ] = "正常";
                strStates[ 1 ] = "异常";
            }
            else if ( type == SensorTypes.开关量 )
            {
                strStates[ 0 ] = "开";
                strStates[ 1 ] = "停";
            }
            /*  压力 = 6,
       压差 = 8, 流量 = 9, 称重 = 20, 转速 = 21, 高度 = 22, 水位 = 23,
       电压 = 24, 电流 = 25, 功率 = 26, 煤位 = 27, 其它_模 = 50*/
            switch ( name.ToUpper( ) )
            {
                case "甲烷":
                case "低浓度甲烷":
                case "高低浓度甲烷":
                case "瓦斯":
                case "CH4":
                case "ch4":
                    typeName = Types.甲烷;
                    unit = "%";
                    break;

                case "高浓瓦斯":
                case "高浓甲烷":
                case "管道高浓":
                case "高浓度甲烷":
                case "管道高浓度甲烷":
                    typeName = Types.高浓甲烷;
                    unit = "%";
                    break;

                case "CO":
                case "一氧化碳":
                case "ＣＯ":
                    typeName = Types.CO;
                    unit = "ppm";
                    break;

                case "CO2":
                case "二氧化碳":
                    typeName = Types.CO2;
                    break;

                case "O2":
                case "氧气":
                    typeName = Types.氧气;
                    unit = "%";
                    break;

                case "浓度":
                    typeName = Types.浓度;
                    break;

                case "H2S":
                case "硫化氢":
                    typeName = Types.H2S;
                    unit = "ppm";
                    break;

                case "风速":
                    typeName = Types.风速;
                    unit = "m/s";
                    break;

                case "风量":
                    typeName = Types.风量;

                    //unit = "m/s";
                    break;

                case "风压":
                case "负压":
                case "压差":
                    typeName = Types.负压;
                    if ( unit.ToLower( ) == "mpa" )
                    {
                        // this.k = 1000F;
                    }
                    unit = "kPa";
                    break;

                case "温度":
                case "轴温":
                case "水温":
                    typeName = Types.温度;
                    unit = "℃";
                    break;

                case "湿度":
                    typeName = Types.湿度;
                    unit = "%";
                    break;

                case "压力":
                case "管道压力":
                    typeName = Types.压力;
                    break;

                case "矿压":
                case "顶板压力":
                    typeName = Types.顶板压力;
                    unit = "TN/M2";
                    break;

                case "粉尘":
                case "矿尘":
                case "洒水降尘":
                    typeName = Types.粉尘;
                    unit = "mg/m3";
                    break;

                case "水位":
                case "液位":
                    typeName = Types.水位;
                    break;

                case "煤位":
                    typeName = Types.煤位;
                    break;

                case "高度":
                case "料位":
                    typeName = Types.高度;
                    break;

                case "位移":
                    typeName = Types.位移;
                    break;

                case "速度":
                    typeName = Types.速度;
                    break;

                case "电量":
                case "电池电量":
                    typeName = Types.电量;
                    break;

                case "电流":
                case "电流互感":
                    typeName = Types.电流;
                    break;

                case "电压":
                case "电压互感":
                    typeName = Types.电压;
                    break;

                case "功率":
                case "有功":
                case "有功功率":
                    typeName = Types.功率;
                    break;

                case "无功":
                case "无功功率":
                    typeName = Types.无功功率;
                    break;

                case "功率因数":
                    typeName = Types.功率因数;
                    break;

                case "计时":
                    typeName = Types.计时;
                    break;

                case "计数":
                    typeName = Types.计数;
                    break;

                case "频率":
                    typeName = Types.频率;
                    break;

                case "混量":
                case "日混量":
                case "混合量":
                case "混合流量":
                    typeName = Types.混量;
                    break;

                case "纯量":
                case "纯流量":
                case "日纯量":
                case "瓦斯量":
                    typeName = Types.纯量;
                    break;

                case "勾数":
                case "钩数":
                    typeName = Types.勾数;
                    break;

                case "人数":
                    typeName = Types.人数;
                    break;

                case "产量":
                    typeName = Types.产量;
                    break;

                case "重量":
                case "称重":
                    typeName = Types.重量;
                    break;

                case "其它_模":
                case "阀门开度":
                case "其它模拟量":
                    typeName = Types.模拟量;
                    break;

                case "流量":
                case "管道流量":
                case "工矿流量":
                case "标准流量":
                    typeName = Types.流量;
                    break;

                case "主通风机":
                case "主扇":
                case "备扇":
                case "副扇":
                    typeName = Types.主扇;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "开";
                    strStates[ 1 ] = "停";
                    m_bTriState = true;
                    break;

                case "风机":
                    typeName = Types.风机;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "开";
                    strStates[ 1 ] = "停";

                    //m_bTriState = true;
                    break;

                case "局部通风机":
                case "局扇":
                    typeName = Types.局扇;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "开";
                    strStates[ 1 ] = "停";
                    break;

                case "风门":
                    typeName = Types.风门;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "关";
                    strStates[ 1 ] = "开";
                    break;

                case "风筒":
                    typeName = Types.风筒;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "有风";
                    strStates[ 1 ] = "无风";
                    break;

                case "抽放泵":
                    typeName = Types.抽放泵;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "开";
                    strStates[ 1 ] = "停";
                    break;

                case "加压泵":
                case "水泵":
                    typeName = Types.水泵;
                    type = SensorTypes.开关量;
                    break;

                case "烟雾":
                    typeName = Types.烟雾;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "无烟";
                    strStates[ 1 ] = "有烟";
                    m_bTriState = true;
                    break;

                case "分站":
                    typeName = Types.分站;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "异常";
                    type = SensorTypes.开关量;
                    break;

                case "给煤机":
                    typeName = Types.给煤机;
                    type = SensorTypes.开关量;
                    break;

                case "机组":
                case "采煤机":
                case "综采机":
                    typeName = Types.采煤机;
                    type = SensorTypes.开关量;
                    break;

                case "运输机":
                    typeName = Types.运输机;
                    type = SensorTypes.开关量;
                    break;

                case "提升机":
                case "绞车":
                    typeName = Types.提升机;
                    type = SensorTypes.开关量;
                    break;

                case "开关":
                    typeName = Types.开关;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "开";
                    strStates[ 1 ] = "关";
                    break;

                case "其它_开":
                case "其它开关量":
                case "开停":

                    typeName = Types.开停;
                    type = SensorTypes.开关量;
                    break;

                case "皮带":
                case "皮带机":
                    typeName = Types.皮带;
                    type = SensorTypes.开关量;
                    break;

                case "状态":
                    typeName = Types.状态;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "异常";
                    break;

                case "报警":
                    typeName = Types.报警;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "报警";
                    break;

                case "故障":
                    typeName = Types.故障;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "无";
                    strStates[ 1 ] = "有";
                    break;

                case "超速":
                    typeName = Types.超速;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "超速";
                    break;

                case "超时":
                    typeName = Types.超时;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "超时";
                    break;

                case "超员":
                    typeName = Types.超员;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "超员";
                    break;

                case "越界":
                    typeName = Types.越界;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "越界";
                    break;

                case "堵煤":
                    typeName = Types.堵煤;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "无";
                    strStates[ 1 ] = "堵煤";
                    break;

                case "纵撕":
                    typeName = Types.纵撕;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "无";
                    strStates[ 1 ] = "纵撕";
                    break;

                case "跑偏":
                    typeName = Types.跑偏;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "无";
                    strStates[ 1 ] = "跑偏";
                    break;

                case "刀闸":
                    typeName = Types.刀闸;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "分闸";
                    strStates[ 1 ] = "合闸";
                    break;

                case "闭锁":
                case "继电器":

                    //typeName = Types.继电器;
                    //type = SensorTypes.控制量;
                    //strStates[0] = "正常";
                    //strStates[1] = "异常";
                    //break;
                    typeName = Types.闭锁;
                    type = SensorTypes.控制量;
                    strStates[ 0 ] = "吸合";
                    strStates[ 1 ] = "断开";
                    break;

                case "断电器":
                case "断电":
                    typeName = Types.断电;
                    type = SensorTypes.控制量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "断电";

                    //m_bTriState = true;
                    break;

                case "馈电器":
                case "馈电":
                    typeName = Types.馈电器;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "有电";
                    strStates[ 1 ] = "无电";
                    break;

                case "供电":
                case "供电方式":
                    typeName = Types.供电;
                    type = SensorTypes.开关量;
                    strStates[ 0 ] = "交流";
                    strStates[ 1 ] = "直流";
                    break;

                case "控制器":
                case "控制":
                    typeName = Types.控制器;
                    type = SensorTypes.控制量;
                    strStates[ 0 ] = "正常";
                    strStates[ 1 ] = "异常";
                    break;

                case "累计量":
                    type = SensorTypes.计数量;
                    typeName = Types.模拟量;
                    break;

                default:
                    if ( unit == "" )
                    {
                        typeName = Types.开停;
                        type = SensorTypes.开关量;
                        strStates[ 0 ] = "开";
                        strStates[ 1 ] = "停";
                    }
                    else
                    {
                        typeName = GetTypesByTypeName( name );
                        type = SensorTypes.模拟量;
                        if ( typeName == Types.无 )
                        {
                            typeName = Types.模拟量;
                        }
                    }
                    break;
            }
            if ( type == SensorTypes.控制量 && strStates[ 0 ] == null )
            {
                typeName = Types.控制;

                //type = SensorTypes.开关量;
                strStates[ 0 ] = "正常";
                strStates[ 1 ] = "异常";
            }
            else if ( type == SensorTypes.开关量 && strStates[ 0 ] == null )
            {
                strStates[ 0 ] = "开";
                strStates[ 1 ] = "停";
            }
            return typeName;
        }

        public static SensorTypes getSensorType( string type )
        {
            MonitoringUnitType.SensorTypes t = MonitoringUnitType.SensorTypes.模拟量;
            switch ( type )
            {
                case "模拟量":
                    t = SensorTypes.模拟量;
                    break;

                case "开关量":
                    t = SensorTypes.开关量;
                    break;

                case "累计量":
                    t = SensorTypes.累计量;
                    break;

                case "控制量":
                    t = SensorTypes.控制量;
                    break;

                case "计数量":
                    t = SensorTypes.计数量;
                    break;

                case "调节量":
                    t = SensorTypes.调节量;
                    break;
            }
            return t;
        }

        public static MonitoringUnitType.Types GetConvertType( string sType , string sensorName , string sensorUnit , ref BaseMonitoringUnit monitoringUint , ref MonitoringUnitType.SensorTypes sensorType )
        {
            MonitoringUnitType.Types type;
            string[ ] strStates = new string[ 3 ];
            string unit = sensorUnit;
            bool m_bTriState = false;
            type = MonitoringUnitType.GetMonitoringUnitTypeByName( sensorName , ref sensorType , ref unit , ref strStates , ref m_bTriState );
            monitoringUint.m_bTriState = m_bTriState;
            if ( sType == "开关量" || sType == "控制量" )
            {
                if ( monitoringUint.m_strStateName == null )
                {
                    monitoringUint.m_strStateName = new string[ 3 ];
                }
                monitoringUint.m_strStateName[ 0 ] = strStates[ 0 ];
                monitoringUint.m_strStateName[ 1 ] = strStates[ 1 ];
                monitoringUint.m_strStateName[ 2 ] = strStates[ 1 ];
            }
            else
            {
                monitoringUint.m_strUnit = unit;
            }
            return type;
        }
    }
}